package com.thesis.manage.service.impl;

import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.mongodb.impl.AbstractMongoServiceImpl;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.StudentTeacher;
import com.thesis.manage.domain.StudentThesis;
import com.thesis.manage.domain.Thesis;
import com.thesis.manage.service.StudentTeacherService;
import com.thesis.manage.service.StudentThesisService;
import com.thesis.manage.service.ThesisService;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.model.*;
import com.thesis.manage.service.module.vo.StudentThesisVO;
import com.thesis.manage.service.module.vo.StudentVO;
import com.thesis.manage.service.module.vo.ThesisVO;
import com.thesis.manage.service.module.vo.TimeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author php
 * @date 2018/3/14
 */
@Service
public class StudentThesisServiceImpl extends AbstractMongoServiceImpl<StudentThesis> implements StudentThesisService {

    @Resource
    private ThesisService thesisService;
    @Resource
    private StudentTeacherService studentTeacherService;

    @Override
    protected Class<StudentThesis> getEntityClass() {
        return StudentThesis.class;
    }

    @Override
    public StudentThesisVO findByStudentId(Long id) {
        StudentThesisVO studentThesisVO = null;
        Assert.notNull(id, ErrorMessage.PARAMS_IS_NULL);
        /*
          查询用户课题
         */
        StudentThesis studentThesis = new StudentThesis();
        studentThesis.setStudentId(id);
        StudentThesis one = this.findByOne(studentThesis);
        if (one != null) {
            /*
             * 查询课题是否存在
             */
            Thesis thesis = this.thesisService.findById(one.getThesisId());
            if (thesis == null) {
                throw new CommonException("student thesis info error", "课题信息有误");
            }
            studentThesisVO = new StudentThesisVO();
            studentThesisVO.setId(one.getId());
            studentThesisVO.setThesisName(thesis.getName());
            /*
             * 选题审核状态
             */
            studentThesisVO.setStatus(ExamineEnum.getName(one.getStatus()));
            studentThesisVO.setApplyTime(one.getStartTime());
            studentThesisVO.setType(ThesisTypeEnum.getName(thesis.getType()));
        }
        return studentThesisVO;
    }

    @Override
    public void selectThesis(Long thesisId, Long studentId) {
        //时间验证
        Result result = thesisService.checkOpenApplication(TimeTypeEnum.APPLY.getValue());
        TimeVO timeVO = (TimeVO) result.getData();
        if (timeVO.getType().intValue() == AnswerStateEnum.NO_OPEN.getValue()) {
            throw new CommonException("no open", "选题未开启");
        }
        if (timeVO.getType().intValue() == AnswerStateEnum.END.getValue()) {
            throw new CommonException("end", "选题已结束");
        }
        Assert.state(thesisId != null && studentId != null, ErrorMessage.PARAMS_IS_NULL);
        if (this.isExist(thesisId)) {
            throw new CommonException("thesis selected", "课题已被选");
        }
        StudentThesis studentThesis = new StudentThesis();
        studentThesis.setStudentId(studentId);
        studentThesis.setThesisId(thesisId);
        studentThesis.setComplete(YesOrNoEnum.NO.getValue());
        studentThesis.setStatus(ExamineEnum.TEACHER_EXAMINEING.getValue());
        studentThesis.setYear(LocalDate.now().getYear());
        studentThesis.setInvalid(YesOrNoEnum.YES.getValue());
        this.insert(studentThesis);
    }

    @Override
    public Page<ThesisVO> showThesisList(Pageable pageable, String studentNumber, Long studentId) {
        //通过学生号查询指导教师
        StudentTeacher studentTeacher = this.studentTeacherService.findByStudentNumber(studentNumber);
        if (studentTeacher == null) {
            throw new CommonException("guide teacher is null", "你还没有指导教师");
        }
        return this.thesisService.showThesisList(studentTeacher.getTeacherNumber(), studentId, pageable);
    }

    @Override
    public List<ThesisVO> reviewThesis(String teacherNumber) {
        List<ThesisVO> thesisVOS = new ArrayList<>();
        StudentTeacher studentTeacher = new StudentTeacher();
        studentTeacher.setTeacherNumber(teacherNumber);
        //查询老师的所有学生
        List<StudentVO> list = this.studentTeacherService.findAllByTeacherId(studentTeacher);
        List<Thesis> theses = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            Thesis thesis;
            for (StudentVO studentVO : list) {
                thesis = new Thesis();
                thesis.setPersonId(studentVO.getId());
                thesis.setStatus(ExamineEnum.TEACHER_EXAMINEING.getValue());
                Thesis thesis1 = this.thesisService.findByOne(thesis);
                if (thesis1 != null) {
                    theses.add(thesis1);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(theses)) {
            ThesisVO thesisVO;
            for (Thesis t : theses) {
                thesisVO = new ThesisVO();
                thesisVO.setDate(t.getStartTime());
                thesisVO.setType(ThesisTypeEnum.getName(t.getType()));
                thesisVO.setPerson(t.getPerson());
                thesisVO.setPersonType(RoleEnum.getName(t.getPersonType()));
                thesisVO.setId(t.getId());
                thesisVO.setName(t.getName());
                thesisVOS.add(thesisVO);
            }
        }
        return thesisVOS;
    }

    @Override
    public void adopt(Long id, Integer status, String message) {
        Thesis thesis = new Thesis();
        thesis.setId(id);
        if (status == YesOrNoEnum.NO.getValue().intValue()) {
            thesis.setStatus(ExamineEnum.EXAMINE_FAILED.getValue());
        } else {
            thesis.setStatus(ExamineEnum.LEADE_EXAMINEING.getValue());
        }
        this.thesisService.update(thesis);
    }

    @Override
    public Boolean exist(Long id) {
        return this.isExist(id);
    }

    @Override
    public void selectThesisAdopt(Long id, Integer status, String message, String teacherName) {
        Assert.state(id != null && status != null, ErrorMessage.PARAMS_IS_NULL);
        StudentThesis studentThesis = this.findById(id);
        if (studentThesis == null) {
            throw new CommonException("student select thesis not exist", "学生选题不存在，请联系相关人员");
        }
        StudentThesis thesis = new StudentThesis();
        thesis.setId(studentThesis.getId());
        thesis.setMessage(message);
        if (status == YesOrNoEnum.NO.getValue().intValue()) {
            thesis.setStatus(ExamineEnum.EXAMINE_FAILED.getValue());
            thesis.setInvalid(YesOrNoEnum.NO.getValue());
        } else {
            thesis.setStatus(ExamineEnum.EXAMINE_SUCCESS.getValue());
        }
        thesis.setAdoptPerson(teacherName);
        this.update(thesis);
    }

    @Override
    public Thesis myThesis(Long studentId) {
        StudentThesis studentThesis = new StudentThesis();
        studentThesis.setStudentId(studentId);
        studentThesis.setStatus(ExamineEnum.EXAMINE_SUCCESS.getValue());
        studentThesis.setInvalid(YesOrNoEnum.YES.getValue());
        //查询我的论题
        StudentThesis var1 = this.findByOne(studentThesis);
        if (var1 != null) {
            //查询论题详细信息
            return this.thesisService.findById(var1.getThesisId());
        }
        return null;
    }

    /**
     * 查看数据是否存在
     *
     * @param thesisId 课题id
     */
    private boolean isExist(Long thesisId) {
        StudentThesis studentThesis = new StudentThesis();
        studentThesis.setThesisId(thesisId);
        //未完成
        studentThesis.setComplete(YesOrNoEnum.NO.getValue());
        //有效--》已被选择
        studentThesis.setInvalid(YesOrNoEnum.YES.getValue());
        return this.findByOne(studentThesis) != null;

    }
}
